﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            /*绑定两个label数据*/
            depthValueLabel.DataBindings.Add("Text", depthSlider, "Value", false, DataSourceUpdateMode.OnPropertyChanged);
            lengthValueLabel.DataBindings.Add("Text", lengthSlider, "Value", false, DataSourceUpdateMode.OnPropertyChanged);
        }
        /*draw按钮触发事件*/
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (graphics == null) graphics = this.CreateGraphics();
                else
                {
                    graphics.Clear(Color.FromArgb(240, 240, 240));
                    graphics.Dispose();
                    graphics = this.CreateGraphics();
                }
                length = lengthSlider.Value * 10;
                depth = depthSlider.Value;
                drawCayleyTree(depth, 200, 310, length, -Math.PI / 2);
            }
            catch { }
        }
        /*全局参量的设定*/
        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;
        int length = 75;
        int depth = 8;
        /*递归函数*/
        void drawCayleyTree(int n, double x0, double y0, double leng, double th)
        {
            try {
                if (n == 0) return;

                double x1 = x0 + leng * Math.Cos(th);
                double y1 = y0 + leng * Math.Sin(th);

                drawLine(x0, y0, x1, y1);

                drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
                drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
            }
            catch { }
         }
        /*画笔函数*/
        void drawLine(double x0, double y0, double x1, double y1)
        {
            try
            {
                Pen pen1;
                if (colorBox.SelectedIndex == 0)
                {
                    pen1 = new Pen(Color.Red, 2);
                    graphics.DrawLine(
                        pen1, (int)x0, (int)y0, (int)x1, (int)y1);
                }
                else if (colorBox.SelectedIndex == 1)
                {
                    pen1 = new Pen(Color.Black, 2);
                    graphics.DrawLine(
                            pen1,
                            (int)x0, (int)y0, (int)x1, (int)y1);
                }
                else if (colorBox.SelectedIndex == 2)
                {
                    pen1 = new Pen(Color.Orange, 2);
                    graphics.DrawLine(
                            pen1,
                            (int)x0, (int)y0, (int)x1, (int)y1);
                }
                else
                {
                    pen1 = new Pen(Color.Blue, 2);
                    graphics.DrawLine(
                        pen1,
                        (int)x0, (int)y0, (int)x1, (int)y1);
                }
            }
            catch { }
        }

        private void per1Slider_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                double sliderPercentage = Convert.ToDouble(per1Slider.Value) / Convert.ToDouble(10);
                double per1Value = 0.6 + sliderPercentage * 0.3;
                per1 = per1Value;
            }
            catch { }
        }


        private void per2Slider_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                double sliderPercentage = Convert.ToDouble(per2Slider.Value) / Convert.ToDouble(10);
                double per2Value = 0.6 + sliderPercentage * 0.3;
                per2 = per2Value;
            }
            catch { }
        }

        private void th1Slider_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                double sliderPercentage = Convert.ToDouble(th1Slider.Value) / Convert.ToDouble(100);
                double th1Value = 0.523 + sliderPercentage * 3;
                th1 = th1Value;
            }
            catch { }
        }
        private void th2Slider_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                double sliderPercentage = Convert.ToDouble(th2Slider.Value) / Convert.ToDouble(100);
                double th2Value = 0.523 + sliderPercentage * 3;
                th2 = th2Value;
            }
            catch { }
        }
    }
}
